import utilfuncs from '../../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import globalConst from '../../common/globalConst.js';
var global = globalConst.GlobalConst;

import { LGXEnums } from '../../common/sysenum.js';
import { PositionUtil } from '../../common/tools/positionutil.js';

class SingleArr {
    constructor(idx) {
        this.index = idx;
        this.list = [];
    }
}

class CircleArraySvc {
    constructor() {
        this.tSrc2DstGraphMap = [];
        this.rotCenterPt = { x: 0, y: 0 }
        this.sureCenter = false;
    }

    array(dataManger, cntDesc, count, angleSpan) {
        let coordLst = cntDesc.split(",");
        if (coordLst.length != 2) {
            return null;
        }
        dataManger.m_pOperStepService.record(1, []);
        let rst = null;
        let xdesc = coordLst[0];
        let ydesc = coordLst[1];
        let x = parseFloat(xdesc);
        let y = parseFloat(ydesc);
        let mousePt = { x, y };

        this.setRotCenterPt(mousePt);
        this.initData(count, dataManger);
        this.rotateByInputAngle(angleSpan);
        dataManger.m_pOperStepService.record(0, []);
        return rst;
    }

    initData(count, dataManger) {
        let tSelGraphList = dataManger.getSelGraphs();
        for (let n = 0; n < count - 1; n++) {
            let singleArrInfo = new SingleArr(n);
            this.tSrc2DstGraphMap.push(singleArrInfo);
            for (let i = 0; i < tSelGraphList.length; i++) {
                let srcGraph = tSelGraphList[i];
                let dstGraph = dataManger.creatNewGraph4InnerSvc(srcGraph.getType());
                if (dstGraph) {
                    let srcInfo = srcGraph.toInfo();
                    srcInfo.gid = dstGraph.m_gid;
                    srcInfo.rid = dstGraph.m_rid;
                    dstGraph.assignInfo(srcInfo);
                    let t = { src: srcGraph, dst: dstGraph };
                    singleArrInfo.list.push(t);
                }
            }
        }
    }

    setRotCenterPt(pt) {
        this.rotCenterPt.x = pt.x;
        this.rotCenterPt.y = pt.y;
        this.sureCenter = true;
    }


    rotateByInputAngle(angleSpan) {
        console.log("rotateByInputAngle***************");

        let rotCntPt = this.rotCenterPt;
        for (let n = 0; n < this.tSrc2DstGraphMap.length; n++) {
            let singleArrInfo = this.tSrc2DstGraphMap[n];
            let rotAngle = angleSpan * (singleArrInfo.index + 1);
            let rotRad = funcs.degree2Radian(rotAngle);
            for (let i = 0; i < singleArrInfo.list.length; i++) {
                let mirInfo = singleArrInfo.list[i];
                let src = mirInfo.src;
                let dst = mirInfo.dst;
                this.rotate(src, dst, rotCntPt, rotRad);
            }
        }
    }

    rotate(srcGraph, dstGraph, rotCntPt, rotRad) {
        if (srcGraph.m_graphType == LGXEnums.LGXGraphType.Path_type) {
            this.rotateCopyPathGraph(dstGraph, rotCntPt, rotRad);
        }
        else {
            let srcPosCopy = srcGraph.getPosCopy();
            let angle = PositionUtil.rotatePos(srcPosCopy, rotCntPt, rotRad);
            dstGraph.setPos(srcPosCopy);
            dstGraph.setAngle(angle);
        }
        dstGraph.calScope(1);
        this.rotateFill(srcGraph, dstGraph, rotCntPt, rotRad);
    }


    rotateCopyPathGraph(dstGraph, rotCntPt, rotRad) {
        let dstPathLinkList = dstGraph.getPathLinkList();
        for (let i = 0; i < dstPathLinkList.length; i++) {
            let pathLink = dstPathLinkList[i];
            pathLink.rotate(rotCntPt, rotRad);
        }
    }

    rotateFill(srcGraph, dstGraph, rotCntPt, rotAngle) {
        if (!funcs.isBaseUnit(srcGraph.getType())
            && srcGraph.m_graphType != LGXEnums.LGXGraphType.Path_type) {
            return;
        }

        let srcFill = srcGraph.getFillDef();
        if (srcFill.gradientInfo) {
            let rotSrcStPt = funcs.rotate(srcFill.gradientInfo.stPt, rotCntPt, rotAngle);
            let rotSrcEndPt = funcs.rotate(srcFill.gradientInfo.endPt, rotCntPt, rotAngle);

            let dstFill = dstGraph.getFillDef();
            dstFill.setStopPos("st", rotSrcStPt);
            dstFill.setStopPos("end", rotSrcEndPt);
        }
    }
}

export default { CircleArraySvc }